package com.wzc.bigevent.service.impl;

import com.wzc.bigevent.mapper.UploadMapper;
import com.wzc.bigevent.pojo.Result;
import com.wzc.bigevent.service.UploadService;
import com.wzc.bigevent.utils.AliOssUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


@Service
public class UploadServiceImpl implements UploadService {

    @Resource
    private AliOssUtil aliOssUtil;

    @Resource
    private UploadMapper uploadMapper;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @Override
    public Result upload(MultipartFile file) {
        String filePath = null;
        try {
            //  获取原始文件名
            String originalFilename = file.getOriginalFilename();
            // 截取原始文件名的扩展名
            String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 生成新的文件名: UUID + 扩展名
            String newFileName = UUID.randomUUID().toString() + extName;

            // 文件上传请求路径
            filePath = aliOssUtil.upload(file.getBytes(), newFileName);

        }catch(IOException e){
            e.printStackTrace();
            return Result.error("文件上传失败");
        }
        return Result.success(filePath);
    }
}
